﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class marquee : MonoBehaviour
{
    public GameObject marqueeBg;//跑马灯背景
    public GameObject marqueeContent;//跑马灯文本
    float marqueeAlpha;//跑马灯背景透明度
    Vector3 initMarqueePos;//跑马灯初始位置
    private string[] _lobbyContent = new string[4];
    private List<string> replaceParam;
    private Dictionary<string, marqueeItemData> marqueeListData;
    int i = 0;
    float myTime;
    public void Start()
    {
        marqueeDataS2C _marqueeDataS2C = new marqueeDataS2C(testStr);
        marqueeListData=_marqueeDataS2C.getMarqueeList();
        marqueeAlpha = marqueeContent.gameObject.GetComponent<UILabel>().alpha;
        initMarqueePos = marqueeContent.transform.localPosition;
        readMarqueeData();
    }
    public void Update()  // time manager  拎到入口做单例统一管理
    {
        myTime += Time.deltaTime;
        initPaomadeng(myTime);
    
    }
    /// <summary>
    /// 读取跑马灯的数据
    /// </summary>
    private void readMarqueeData()
    {
        //TODO:测试替换， 之后改为读入数据
        string[] arr = new string[] { "1321", "5443", "44454", "545413" };
        replaceParam = new List<string>(arr);
        string esc = @"\{(.*?)\}";
        for (int i = 0; i < marqueeListData.Count; i++)
        {
            _lobbyContent[i] = StringHandler.strReplaceHandler(marqueeListData["marqueeItem" + i].marqueeDes, esc, replaceParam);//_lobbyTextContent[i], replaceParam);
        }

    }
    /// <summary>
    /// 跑马灯
    /// </summary>
    private void initPaomadeng(float time)
    {
        if (time > 2.0f)
        {
            marqueeContent.gameObject.GetComponent<UILabel>().text = _lobbyContent[i];
            marqueeContent.transform.Translate(new Vector3(-Time.deltaTime * 0.4f, 0, 0));
            marqueeContent.gameObject.GetComponent<UILabel>().enabled = true;
            marqueeBg.gameObject.GetComponent<UI2DSprite>().enabled = true;
            if (time > 10.0f)
            {
                marqueeContent.gameObject.GetComponent<UILabel>().alpha = 1 - (time - (int)time);
                marqueeBg.gameObject.GetComponent<UI2DSprite>().alpha = 1 - (time - (int)time);
                if (marqueeContent.gameObject.GetComponent<UILabel>().alpha < 0.1f)
                {
                    marqueeContent.gameObject.GetComponent<UILabel>().enabled = false;
                    marqueeBg.gameObject.GetComponent<UI2DSprite>().enabled = false;
                    marqueeContent.gameObject.GetComponent<UILabel>().alpha = marqueeAlpha;
                    marqueeBg.gameObject.GetComponent<UI2DSprite>().alpha = marqueeAlpha;
                    marqueeContent.transform.localPosition = initMarqueePos;
                    i++;
                    if (i > _lobbyContent.Length - 2) { i = 0; }
                    myTime = 0;
                }
            }
        }
    }
    private string testStr = @"
       {
    ""ret"": 0,
    ""list"": [
        {
            ""marqueeDes"": ""{0}一路血拼，过关斩将，已达成了{1} 连斩，求超越！""
        },
        {
            ""marqueeDes"": ""恭喜{0}已经击败了{1}荣登财富榜{2}位！爆炸头特赐{3}金以资鼓励！""
        },
        {
            ""marqueeDes"": ""{0}于北雁荡山结庐修炼七七四十九天，终于练就{2}称号！""
        }
    ]
}
       ";
}
